package com.imc.platform.base.menu.controller;

import com.imc.core.utils.MenuUtils;
import com.imc.core.utils.MysqlUtil;
import com.imc.platform.base.menu.service.IMenuService;
import com.imc.platform.base.user.pojo.User;
import com.twp.common.tuple.ResultDTO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    IMenuService menuService;

    @Autowired
    MenuUtils menuUtils;

    /**
     *QY 2018/9/3 8:01
     *方法描述：传参对象
     **/
    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    public static class ReqTuple{
        Map<String,Object> parmsMap;
        List<Map<String,Object>> parmsList;
        String menuId;
    }

    @RequestMapping(value = "/showMenuPage",method = RequestMethod.GET)
    public String showMenuPage(String menuId,Model model,HttpServletRequest request){
        model.addAttribute("menu", menuUtils.getMenuByRoleId(request,menuId));
        return "base/menu/menu";
    }

    @GetMapping(value = "/showAddMenuPage")
    public String showAddMenuPage(String menuId, Model model){
        model.addAttribute("menuId",menuId);
        return "base/menu/addMenu";
    }

    @GetMapping(value = "/updateMenuPage")
    public String showUpdateMenuPage(String menuId, Model model){
        model.addAttribute("menuId",menuId);
        return "base/menu/updateMenu";
    }

    @GetMapping(value = "/queryMenuByMenuId")
    @ResponseBody
    public ResultDTO queryMenuByMenuId(String menuId){
        return ResultDTO
                .builder()
                .data(menuService.queryMenuByMenuId(menuId))
                .build();
    }

    @GetMapping(value = "/queryIsNameRep")
    @ResponseBody
    public ResultDTO queryIsNameRep(String menuName){
        return ResultDTO
                .builder()
                .data(menuService.queryIsNameRep(menuName))
                .build();
    }

    @RequestMapping(value = "/addMenu",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO addMenu(@RequestBody ReqTuple reqTuple){
        return ResultDTO
                .builder()
                .data(menuService.addMenu(reqTuple.getParmsMap()))
                .build();
    }

    @RequestMapping(value = "/addMenuFirstLevel",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO addMenuFirstLevel(){
        return ResultDTO
                .builder()
                .data(menuService.addMenuFirstLevel())
                .build();
    }

    @RequestMapping(value = "/updateMenuByMenuId",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO updateMenuByMenuId(@RequestBody ReqTuple reqTuple){
        return ResultDTO
                .builder()
                .data(menuService.updateMenuByMenuId(reqTuple.getParmsMap()))
                .build();
    }

    @RequestMapping(value = "/delMenuById",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO delMenuById(@RequestParam String id,@RequestParam String parentId){
        return ResultDTO
                .builder()
                .data(menuService.delMenuById(id,parentId))
                .build();
    }
}
